<%@ page import="ro.gateway.aida.db.PersistenceToken,
                 ro.gateway.aida.db.PersistenceManager,
                 ro.gateway.aida.obj.db.AIDALocationDB,
                 java.io.BufferedReader,
                 java.io.FileReader,
                 java.io.File,
                 com.sun.msv.verifier.regexp.StringToken,
                 java.util.StringTokenizer,
                 ro.gateway.aida.obj.db.TownDB,
                 ro.gateway.aida.obj.Town,
                 ro.gateway.aida.obj.AIDALocation,
                 ro.gateway.aida.obj.Country,
                 ro.gateway.aida.obj.db.ActivityDB,
                 ro.gateway.aida.db.DBPersistenceToken,
                 ro.gateway.aida.db.ConnectionWrapper"%>

<%
      ConnectionWrapper.DEBUG_LEVEL = 1;
      DBPersistenceToken token = ( DBPersistenceToken ) PersistenceManager.
            tokenLookup( application );
      AIDALocationDB pman = AIDALocationDB.getManager( token );

      BufferedReader reader = new BufferedReader(
            new FileReader( new File( application.getRealPath("/qqq/wriPrj7.txt")))
      );

      int i = 0;
      while (true) {
            out.flush();
            i++;
            String line = reader.readLine();
            if (line == null) break;
            if (i <= 10) continue;
//            if (i > 10) break;

            StringTokenizer st = new StringTokenizer(line, "~");
            String praja_id = null;
            String location = null;
            if (st.hasMoreTokens()) praja_id = st.nextToken();
            if (st.hasMoreTokens()) location = st.nextToken();


            long activity_id = -1;
//            AIDALocation[] ex_locations = null;
            try {
                  activity_id = ActivityDB.getManager(token ).
                        getActivityIdByPrajaId(praja_id);
//                  ex_locations = AIDALocationDB.getManager(token ).
//                        getForActivity(activity_id);

            } catch (Exception ex) {
                  ex.printStackTrace();
            }
            if (activity_id == -1) {
                  out.println("activity not found:" + praja_id + "<br/>");
                  continue;
            }
//            if (ex_locations != null) {
//                  out.println("locations for activity already there:" + praja_id + " : maybe :" + location + "<br/>");
//                  continue;
//            }


            out.println("Praja_id: " + praja_id + "<br>");

            String s_cregion = null;
            String s_country = null;
            String s_county = null;
            String s_town = null;
            st = new StringTokenizer(location, "/");
            if (st.hasMoreTokens()) {
                  s_cregion = st.nextToken();
                  if (st.hasMoreTokens()) {
                        s_country = st.nextToken();
                        if (st.hasMoreTokens()) {
                              s_county = st.nextToken();
                              if (st.hasMoreTokens()) {
                                    s_town = st.nextToken();
                              } else {
                                    s_town = s_county;
                                    s_county = null;
                              }
                        }
                  }
            }

            try {
                  AIDALocation aida_loc = AIDALocationDB.getManager(token ).
                        getOrCreateLocation(s_cregion, s_country, s_county, s_town);
                  if (location == null) {
                        out.println( "<font color=\"red\"> " +
                              s_cregion + "," + s_country + "," + s_county+ "," + s_town +
                              " Not found </font><br>" );
                  } else {
                        out.println( "parsed:" + location );
                        aida_loc.setActivityId(activity_id);
                        AIDALocationDB.getManager(token )
                              .insertLocation(aida_loc);
                        out.println("... done<br>");
                  }
            } catch (Exception ex) {
                  out.println("<font color=\"red\"> failed:" + location + ", " + praja_id + "</font><br>");
                  ex.printStackTrace();
            }

            out.println("Location: " +
                  s_cregion +
                  " - " + s_country +
                  ((s_county != null) ?
                  (" - " + s_county) : "") +
                  " - " + s_town +
                  "<br><br>");
      }
%>